package home

import (
	"html/template"
	"net/http"

	"appengine"
	"appengine/user"

	"auth_handlers"
	"model"
)

func init() {
	http.HandleFunc("/home", auth_handlers.Public(handler))
}

type data struct {
	PABUser   *model.User
	GUser     *user.User
	LogOutURL string
	LoginURL  string
}

func handler(w http.ResponseWriter, r *http.Request, gu *user.User, pabu *model.User) {
	w.Header().Set("Content-type", "text/html; charset=utf-8")

	d := data{
		PABUser: pabu,
		GUser:   gu,
	}

	c := appengine.NewContext(r)
	u := user.Current(c)
	if u != nil {
		d.LogOutURL, _ = user.LogoutURL(c, "/home")
	} else {
		d.LoginURL, _ = user.LoginURL(c, "/home")
	}

	err := homeTemplate.Execute(w, d)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}
}

var homeTemplate = template.Must(template.New("home").Parse(homeTemplateHTML))

const homeTemplateHTML = `
<!DOCTYPE html>
<html>
  <body>
    <p>You're status is</p>
    <pre>{{.}}</pre>
  </body>
</html>
`
